/*
 *  Copyright 2019-2023 oyun
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.oyun.modules.videoandbook.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oyun.modules.videoandbook.domain.Book;
import com.oyun.modules.videoandbook.domain.Video;
import com.oyun.modules.videoandbook.domain.vo.BookQueryCriteria;
import com.oyun.modules.videoandbook.mapper.BookMapper;
import com.oyun.modules.videoandbook.service.BookService;
import com.oyun.segment.convert.itmd.IntermediateCodeUtils;
import com.oyun.utils.FileUtil;
import com.oyun.utils.PageResult;
import com.oyun.utils.PageUtil;
import com.oyun.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author oyun
 * @description 服务实现
 * @date 2024-05-08
 **/
@Service
@RequiredArgsConstructor
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements BookService {

    private final BookMapper bookMapper;

    @Override
    public PageResult<Book> queryAll(BookQueryCriteria criteria, Page<Object> page) {
        if (StringUtils.isNotEmpty(criteria.getName())) {
            criteria.setReplaceAll(IntermediateCodeUtils.mn2Code(criteria.getName()).replaceAll("-", " -"));
        }
        return PageUtil.toPage(bookMapper.findAll(criteria, page));
    }

    @Override
    public List<Book> queryAll(BookQueryCriteria criteria) {
        return bookMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Book resources) {
        convertCode(resources);
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Book resources) {
        Book book = getById(resources.getId());
        book.copy(resources);
        convertCode(book);
        saveOrUpdate(book);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Book> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Book book : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("图书名称", book.getName());
            map.put("图书名称", book.getNameCode());
            map.put("封面路径", book.getCoverPath());
            map.put("作者", book.getAuthor());
            map.put("图书类型(字典)", book.getType());
            map.put("图书介绍", book.getIntroduce());
            map.put("图书分类名称(字典)", book.getClassificationName());
            map.put("共享url", book.getUrl());
            map.put("资源路径", book.getResourcePath());
            map.put("出版商(字典)", book.getPublisher());
            map.put("是否共享(0 不共享 1 共享)", book.getShare());
            map.put("内容", book.getContent());
            map.put("版权", book.getCopyright());
            map.put("页面索引", book.getPageIndex());
            map.put("位置", book.getPosition());
            map.put(" createBy", book.getCreateBy());
            map.put(" createTime", book.getCreateTime());
            map.put(" updateBy", book.getUpdateBy());
            map.put(" updateTime", book.getUpdateTime());
            map.put(" status", book.getStatus());
            map.put("备注", book.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Object getByName(String name) {
        String replaceAll = IntermediateCodeUtils.mn2Code(name).replaceAll("-", " -");
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Book::getNameCode, replaceAll);
        return bookMapper.selectList(wrapper);
    }

    private void convertCode(Book book) {
        book.setNameCode(IntermediateCodeUtils.mn2Code(book.getName()).replaceAll("-", " -"));
    }
}
